﻿using System;
using System.Diagnostics;
using System.Globalization;
using System.Linq;

namespace RatingAuthor
{
  public class BookRecord
  {
    public bool IsValid
    {
      get
      {
        if (AuthorUrl == AuthorUrl.Invalid) return false;
        if (Url == null) return false;
        if (Size == -1) return false;
        return true;
      }
    }
    public AuthorUrl AuthorUrl { get; private set; }
    public string AuthorName { get; private set; }
    public string Url { get; private set; }
    public string Title { get; private set; }
    public int Size { get; private set; }
    public string[] Ganres { get; private set; }
    public double? Mark { get; private set; }
    public int? MarksCount { get; private set; }
    public int? CommentsCount { get; private set; }

    public override string ToString()
    {
      return Title;
    }

    public void SetAuthorUrl(string url)
    {
      AuthorUrl = AuthorUrl.Invalid;

      if (url.StartsWith("/") && url.EndsWith("/"))
      {
        var parts = url.Trim('/').Split('/');
        if (parts.Length == 1)
        {
          return;
        }
        if (parts.Length == 2 && parts[0].Length == 1)
        {
          AuthorUrl = new AuthorUrl(parts[0][0], parts[1]);
          return;
        }
        if (parts.Length == 3)
        {
          return;
        }
      }
      else if (url.StartsWith("\\"))
      {
        var parts = url.TrimStart('\\').Split('\\');
        if (parts.Length == 2 && parts[0].Length == 1)
        {
          AuthorUrl = new AuthorUrl(parts[0][0], parts[1]);
          return;
        }
        if (parts.Length == 3)
        {
          return;
        }
      }
      throw new NotSupportedException();
    }
    public void SetUrl(string value)
    {
      Url = value;
      if (AuthorUrl == AuthorUrl.Invalid) return;

      var t = value.Trim('/');
      var a = AuthorUrl.Effective;

      if (t.StartsWith(a))
      {
        var res = t.Substring(a.Length);
        Debug.Assert(!res.Contains("\\") && !res.Contains("/"));
        if (res.EndsWith(".shtml"))
          res = res.Substring(0, res.Length - ".shtml".Length);
        if (res.Contains("."))
          Debugger.Break();
        Url = res;
        return;
      }
      if (a.StartsWith(t))
      {
        Url = null;
        return;
      }

      throw new NotSupportedException();
    }
    public void SetTitle(string value)
    {
      Title = value.Trim().Replace("&quot;", "\"");
    }
    public void SetSize(string value)
    {
      Size = -1;
      if (value.EndsWith("k"))
      {
        Size = int.Parse(value.TrimEnd('k'), CultureInfo.InvariantCulture);
        return;
      }
      int res;
      if (int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out res))
        Size = res;
    }
    public void SetGanres(string value)
    {
      Ganres = value
        .Replace("&nbsp;", "")
        .Split(',')
        .Select(g => g.Trim())
        .Where(g => g != "")
        .ToArray();
    }
    public void SetAuthorName(string value)
    {
      AuthorName = value.Trim().Replace("&quot;", "\"");
    }
    public void SetMark(string value)
    {
      Mark = double.Parse(value, CultureInfo.InvariantCulture);
    }
    public void SetMarksCount(string value)
    {
      MarksCount = int.Parse(value, CultureInfo.InvariantCulture);
    }
    public void SetCommentsCount(string value)
    {
      CommentsCount = int.Parse(value, CultureInfo.InvariantCulture);
    }
  }
}